<template>

  <van-icon @click="onCollected" :color="collectedProp ? 'green' : '#777'" :name="collectedProp ? 'star':'star-o'" />
</template>

<script>
import { addArticleCollected, deleteArticleCollected } from '@/api/article/index.js'
export default {
  name: 'collected',
  props: {
    collectedProp: {
      type: Boolean,
      required: ' true'
    },
    art_id: {
      type: [Number, Object],
      required: true
    }
  },
  // 配置v-model的解析规则
  model: {
    prop: 'collectedProp'
  },
  data () {
    return {}
  },
  methods: {
    // 收藏的点击事件
    async onCollected () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        if (this.collectedProp) {
          // 已收藏 取消收藏
          await deleteArticleCollected(this.art_id)
          this.$toast('取消收藏成功')
        } else {
          // 未收藏 添加收藏
          await addArticleCollected(this.art_id)
          this.$toast('添加收藏成功')
        }
        this.$emit('input', !this.collectedProp)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
